﻿namespace QLPhongKham.BaseClass.BenhNhan
{
    using ConfigDatabase.Class;
    using Npgsql;
    using System;
    using System.Data;

    public class dtoBNThongTinBN
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private int _iidgioitinh;
        private long _iidphuongxa;
        private int _inamsinh;
        private string _sdiachi;
        private string _sdienthoai;
        private string _shoten;
        private string _smabn;
        private string _sngaysinh;
        private string _sSchema;

        public dtoBNThongTinBN()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoBNThongTinBN(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoBNThongTinBN.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand("delete from " + this._sSchema + "." + tableName + " where  " + columnMABN + " = '" + this._smabn + "'", connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoBNThongTinBN.cs");
                return false;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoBNThongTinBN.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnMABN, ",", columnHOTEN, ",", columnIDGIOITINH, ",", columnNAMSINH, ",", columnDIACHI, ",", columnDIENTHOAI, 
                    ",", columnNGAYSINH, ",", columnIDPHUONGXA, ") values('", this._smabn, "','", this._shoten, "',", this._iidgioitinh, ",", this._inamsinh, ",'", this._sdiachi, "','", this._sdienthoai, 
                    "','", this._sngaysinh, "',", this._iidphuongxa, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoBNThongTinBN.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._smabn = "";
            this._shoten = "";
            this._iidgioitinh = 0;
            this._inamsinh = 0;
            this._sdiachi = "";
            this._sdienthoai = "";
            this._sngaysinh = "";
            this._iidphuongxa = 0L;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoBNThongTinBN.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnMABN + "," + columnHOTEN + "," + columnIDGIOITINH + "," + columnNAMSINH + "," + columnNGAYSINH + "," + columnDIACHI + "," + columnDIENTHOAI + "," + columnIDPHUONGXA + " from " + this._sSchema + "." + tableName + " where " + columnMABN + " = '" + this._smabn + "'", connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._smabn = dataTable.Rows[0][columnMABN].ToString();
                    this._shoten = dataTable.Rows[0][columnHOTEN].ToString();
                    this._iidgioitinh = int.Parse(dataTable.Rows[0][columnIDGIOITINH].ToString());
                    this._inamsinh = int.Parse(dataTable.Rows[0][columnNAMSINH].ToString());
                    this._sdiachi = dataTable.Rows[0][columnDIACHI].ToString();
                    this._sdienthoai = dataTable.Rows[0][columnDIENTHOAI].ToString();
                    this._sngaysinh = dataTable.Rows[0][columnNGAYSINH].ToString();
                    this._iidphuongxa = long.Parse(dataTable.Rows[0][columnIDPHUONGXA].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoBNThongTinBN.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoBNThongTinBN.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable table = new DataTable();
                return this.LoadAll(connect, "");
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoBNThongTinBN.cs");
                return null;
            }
        }

        public DataTable LoadAll(string mabn)
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect, mabn);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoBNThongTinBN.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect, string mabn)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnMABN + "," + columnHOTEN + "," + columnIDGIOITINH + ",case when " + columnIDGIOITINH + "=0 then 'Nam' else 'Nữ' end as phai," + columnNAMSINH + "," + columnNGAYSINH + "," + columnDIACHI + "," + columnDIENTHOAI + "," + columnIDPHUONGXA + " from " + this._sSchema + "." + tableName + " where 1=1 " + ((mabn == "") ? "" : (" and " + columnMABN + "='" + mabn + "'")), connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect,mabn)", "dtoBNThongTinBN.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoBNThongTinBN.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnHOTEN, " = '", this._shoten, "',", columnIDGIOITINH, " = ", this._iidgioitinh, ",", columnNAMSINH, " = ", this._inamsinh, 
                    ",", columnDIACHI, " = '", this._sdiachi, "',", columnDIENTHOAI, " = '", this._sdienthoai, "',", columnNGAYSINH, "='", this._sngaysinh, "',", columnIDPHUONGXA, "=", this._iidphuongxa, 
                    " where ", columnMABN, " = '", this._smabn, "'"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoBNThongTinBN.cs");
                return false;
            }
        }

        public static string columnDIACHI
        {
            get
            {
                return "diachi";
            }
        }

        public static string columnDIENTHOAI
        {
            get
            {
                return "dienthoai";
            }
        }

        public static string columnHOTEN
        {
            get
            {
                return "hoten";
            }
        }

        public static string columnIDGIOITINH
        {
            get
            {
                return "idgioitinh";
            }
        }

        public static string columnIDPHUONGXA
        {
            get
            {
                return "idphuongxa";
            }
        }

        public static string columnMABN
        {
            get
            {
                return "mabn";
            }
        }

        public static string columnNAMSINH
        {
            get
            {
                return "namsinh";
            }
        }

        public static string columnNGAYSINH
        {
            get
            {
                return "ngaysinh";
            }
        }

        public string DIACHI
        {
            get
            {
                return this._sdiachi;
            }
            set
            {
                this._sdiachi = value;
            }
        }

        public string DIENTHOAI
        {
            get
            {
                return this._sdienthoai;
            }
            set
            {
                this._sdienthoai = value;
            }
        }

        public string HOTEN
        {
            get
            {
                return this._shoten;
            }
            set
            {
                this._shoten = value;
            }
        }

        public int IDGIOITINH
        {
            get
            {
                return this._iidgioitinh;
            }
            set
            {
                this._iidgioitinh = value;
            }
        }

        public long IDPHUONGXA
        {
            get
            {
                return this._iidphuongxa;
            }
            set
            {
                this._iidphuongxa = value;
            }
        }

        public string MABN
        {
            get
            {
                return this._smabn;
            }
            set
            {
                this._smabn = value;
            }
        }

        public int NAMSINH
        {
            get
            {
                return this._inamsinh;
            }
            set
            {
                this._inamsinh = value;
            }
        }

        public string NGAYSINH
        {
            get
            {
                return this._sngaysinh;
            }
            set
            {
                this._sngaysinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "bn_thongtinbn";
            }
        }
    }
}

